home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Practical Algorithms for Image Analysis
/
Practical Algorithms for Image Analysis.iso
/
TARFILE.GZ
/
tarfile
/
ch_3.6
/
subsample
/
convolve1r.c
next >
Wrap
C/C++ Source or Header
|
1999-09-11
|
3KB
|
76 lines
/*
* convolve1r.c
*
* Practical Algorithms for Image Analysis
*
* Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
*/
/* CONVOLVE1R: function performs 2-D convolution by separable,
* symmetric, 1-D filter mask, then outputs subsample image
* usage: convolve1r (imgI, imgO, fltr1D, nFltr)
*/
#include <images.h>
#include <tiffimage.h>
void
convolve1r (imgI, imgO, fltr1D, nFltr, rSubsample)
Image *imgI; /* input image structure */
Image *imgO; /* output image structure */
long *fltr1D; /* 1-D of filter mask */
long nFltr; /* number of filter coefficients */
long rSubsample; /* subsample rate */
{
Image *imgT; /* intermediate image structure */
unsigned char **imgIn, /* input image */
**imgTi, /* intermediate image */
**imgOut; /* output image */
long width, height; /* size of image */
long sumFltr; /* sum of fltr coefficients */
long sum; /* sum of filter convolution at a pixel */
long midFltr; /* middle coefficient index of filter */
long xEnd, yEnd; /* end coefficients of convolution */
long xOut, yOut; /* output image coordinates */
long x, y, i;
/* initialize images */
imgIn = ImageGetPtr (imgI);
height = ImageGetHeight (imgI);
width = ImageGetWidth (imgI);
imgT = ImageAlloc (height, width, 8);
imgTi = ImageGetPtr (imgT);
imgOut = ImageGetPtr (imgO);
/* find sum of filter coefficients */
for (i = 0, sumFltr = 0; i < nFltr; i++)
sumFltr += fltr1D[i];
/* perform row-wise convolution */
midFltr = (nFltr - 1) / 2;
xEnd = width - midFltr;
for (y = 0; y < height; y++) {
for (x = midFltr; x < xEnd; x += rSubsample) {
sum = fltr1D[midFltr] * imgIn[y][x];
for (i = 1; i <= midFltr; i++)
sum += fltr1D[i + midFltr]
* (imgIn[y][x - i] + imgIn[y][x + i]);
imgTi[y][x] = (unsigned char) (sum / sumFltr);
}
}
/* perform column-wise convolution */
yEnd = height - midFltr;
for (y = midFltr, yOut = 0; y < yEnd; y += rSubsample) {
for (x = midFltr, xOut = 0; x < xEnd; x += rSubsample) {
sum = fltr1D[midFltr] * imgTi[y][x];
for (i = 1; i <= midFltr; i++)
sum += fltr1D[i + midFltr] * (imgTi[y - i][x] + imgTi[y + i][x]);
imgOut[yOut][xOut++] = (unsigned char) (sum / sumFltr);
}
yOut++;
}
return;
}